package zhangcong.dao;

import org.springframework.transaction.annotation.Transactional;
import zhangcong.entity.BjEntity;
import zhangcong.entity.Page;
import zhangcong.entity.ZhangCongEntity;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import java.util.List;

@Component()
public class ZhangCongDao  extends HibernateDaoSupport {


    public String login(ZhangCongEntity zhangCongEntity) {
        String sql = "select * from j03zhangcong where name =? and password=?";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setParameter(0,zhangCongEntity.getName() );
        query.setParameter(1,zhangCongEntity.getPassword() );
        List<ZhangCongEntity> list=  query.list();

        if(list.size() == 0){

            return "0";
        }else {

            return "1";
        }
    }




    public void save(ZhangCongEntity zhangCongEntity) {
        Session session= getSessionFactory().openSession();
        session.saveOrUpdate(zhangCongEntity);
        session.flush();
    }




    public int queryCount(String name) {
        String sql = "select * from j03zhangcong where name like ? ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setParameter(0,  "%" + name + "%");
        List<ZhangCongEntity> list=  query.list();
        return  list.size();
    }

    public List<ZhangCongEntity> queryPage(Page page, String name) {
        String sql = "select a.name,a.id,a.url,b.bname from j03zhangcong a,j03zcclazz b where a.bid=b.id and  name like ? order by systime desc limit ?,? ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setParameter(0,  "%" + name + "%");
        query.setParameter(1,  page.getBeginIndex());
        query.setParameter(2,  page.getEveryPage());
        query.setResultTransformer(Transformers.aliasToBean(ZhangCongEntity.class));
        List<ZhangCongEntity> list=  query.list();
        return  list;
    }

    public ZhangCongEntity edit(ZhangCongEntity zc) {
        Session session= getSessionFactory().openSession();
        return (ZhangCongEntity)session.get(ZhangCongEntity.class,zc.getId());
    }

    public void del(ZhangCongEntity zhangCongEntity) {
        Session session= getSessionFactory().openSession();
        session.delete(zhangCongEntity);
        session.flush();
    }

    public List<BjEntity> bj() {
        String sql = "select * from j03zcclazz  ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setResultTransformer(Transformers.aliasToBean(BjEntity.class));
        List<BjEntity> list=  query.list();
        return  list;
    }
}
